package com.edu.dao.impl;

import com.edu.dao.BaseDao;
import com.edu.dao.informationModificationDao;
import com.edu.entity.User;
import com.edu.entity.userFollow;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.SQLException;

public class informationModificationDaoimpl implements informationModificationDao {
    private BaseDao bd = new BaseDao();
    
    @Override
    public boolean updateUserinformation(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        
        try {
            conn = bd.getConn();
            String sql = "UPDATE user SET username=?, sex=?, phoneNum=?, "
                    + "brithday=?, address=?, introduce=?, headImg=?, password=? "
                    + "WHERE userId=?";
            
            ps = conn.prepareStatement(sql);
            
            ps.setString(1, user.getUsername());
            ps.setInt(2, user.getSex());
            ps.setString(3, user.getPhonenum());
            ps.setDate(4, user.getBrithday() != null ? 
                new java.sql.Date(user.getBrithday().getTime()) : null);
            ps.setString(5, user.getAddress());
            ps.setString(6, user.getIntroduce());
            if (user.getHeadimg() == null) {
                user.setHeadimg("/default-avatar.png"); // 设置默认头像路径
            }
            ps.setString(7, user.getHeadimg());
            ps.setString(8, user.getPassword());
            ps.setInt(9, user.getUserid());
            
            int result = ps.executeUpdate();
            return result > 0;
            
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        } finally {
            bd.closeAll(conn, ps);
        }
    }
}